// pages/comp-prod-list/comp-prod-list.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: {}, //首页数据
    AssTypeIdx: '', //行业选择
    AssIdx: '', //公司搜索
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCompanyArea(); //协会搜索行业分类
    this.getCompanyList(); //协会搜索公司分类
    this.searchProduct(); //搜索
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getIndexData(); //获取首页数据

  },



  getIndexData: function () { //获取首页数据
    var that = this;
    var user_id = wx.getStorageSync('userId')
    console.log('user_id====' + user_id)
    app.request(app.globalData.host + 'index/index', { user_id }, 'GET', false,
      function (res) {
        wx.hideLoading();
        console.log(res)
        var index = res.data.data;
        console.log(index)
        that.setData({
          index,
        })
      },
      function (err) { },
      function (complete) { }
    )
  },

  bindPickerAssocType: function (e) { //行业选择
    var that = this;
    var AssTypeIdx = e.detail.value;
    that.setData({
      AssTypeIdx
    })
    that.searchProduct(); //搜索

  },

  bindPickerAssoc: function (e) { //企业选择
    var that = this;
    var AssIdx = e.detail.value;
    that.setData({
      AssIdx
    })
    that.searchProduct(); //搜索

  },

  getCompanyArea: function () { //搜索行业分类
    var that = this;
    wx.request({
      url: app.globalData.host + 'company/getCompanyArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        wx.setStorageSync('AssocType', res.data.data)
        that.setData({
          AssocType: res.data.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  getCompanyList: function () { //搜索企业分类
    var that = this;
    wx.request({
      url: app.globalData.host + 'company/getCompanyList',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        that.setData({
          Assoc: res.data.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  searchProduct: function () { //搜索！
    var that = this;
    var { AssocType, AssTypeIdx, Assoc, AssIdx } = that.data;
    var typeid=''
    if(AssTypeIdx==''){
      typeid=''
    }else{
      typeid= AssocType[AssTypeIdx].id
    }
    var companyid=''
    if(AssIdx==''){
      companyid=''
    }else{
      companyid= Assoc[AssIdx].id
    }
    // var typeid = AssocType[AssTypeIdx].id||'';
    // var companyid = Assoc[AssIdx].id||'';
    wx.request({
      url: app.globalData.host + 'index/searchProduct',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { typeid, companyid },
      method: 'GET',
      success: function (res) {
        that.setData({
          'index.product': res.data.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  openProductDetail: function (e) { //打开产品详情
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/product-detail/product-detail?id=' + id,
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})